package com.hyj.ecp.backstage.order.action;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.ecp.order.OrderQO;
import com.hyj.ecp.backstage.order.service.IBackOrderService;
import com.hyj.ecp.common.constant.SysConfig;

@Controller
@SessionAttributes("user")
@RequestMapping("/backstage/order/*")
public class BackOrderAction {

	Log logger = LogFactory.getLog(BackOrderAction.class);
	@Autowired
    private IBackOrderService service;
	
	@Autowired
    private SysConfig sysconfig;
	
	@RequestMapping(value="toOrderList.do")
    public String toOrderList(OrderQO qo,ModelMap map) throws IOException
    {
    	logger.info("toOrderList.do");

    	List<Map<String,Object>> orderStatus = sysconfig.querConfig("orderstatus");
    	map.put("orderStatus", orderStatus);
    	
		return "view/backstage/order/orderList";
    	
    }
	
	/**
	 * 积分订单查询界面
	 * @param qo
	 * @param map
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value="toPointOrderList.do")
    public String toPointOrderList(OrderQO qo,ModelMap map) throws IOException
    {
    	logger.info("toPointOrderList.do");

    	List<Map<String,Object>> orderStatus = sysconfig.querConfig("orderstatus");
    	map.put("orderStatus", orderStatus);
    	
		return "view/backstage/order/pointOrderList";
    	
    }
	
	
	/**
	 * 查询货币订单
	 * @param qo
	 * @return
	 */
    @ResponseBody
	@RequestMapping(value="queryOrder.do")
    public Object queryOrder(OrderQO qo)
    {
    	logger.info("queryOrder.do  start...");
        qo.setType("1");
    	Map<String,Object> vo = service.queryOrder(qo);
    	return vo;
    }
    
    /**
	 * 查询积分订单
	 * @param qo
	 * @return
	 */
    @ResponseBody
	@RequestMapping(value="queryPointOrder.do")
    public Object queryPointOrder(OrderQO qo)
    {
    	logger.info("queryPointOrder.do  start...");
        qo.setType("2");
    	Map<String,Object> vo = service.queryOrder(qo);
    	return vo;
    }
    
    @RequestMapping(value="toOrderInfo.do")
    public String toOrderInfo(OrderQO qo,ModelMap map) throws IOException
    {
    	logger.info("toOrderInfo.do ...");
    	
    	List<Map<String,Object>> list = service.queryOrderInfo(qo);
    	map.put("list", list);
		return "view/backstage/order/orderInfo";
    }
    
    @RequestMapping(value="toUpdateOrderStatus.do")
    public String toUpdateOrderStatus(OrderQO qo,ModelMap map) throws IOException
    {
    	logger.info("toUpdateOrderStatus.do ...");
    	
    	List<Map<String,Object>> list = service.queryOrderInfo(qo);
    	map.put("list", list);
    	List<Map<String,Object>> orderStatus = sysconfig.querConfig("orderstatus");
    	map.put("orderStatus", orderStatus);
		return "view/backstage/order/updateOrderStatus";
    }
   
    
    /**
     * 更新 订单状态
     * @param qo
     * @return
     */
    @ResponseBody
	@RequestMapping(value="updateOrderInfo.do")
    public Object updateOrderInfo(OrderQO qo)
    {
    	logger.info("updateOrderInfo.do  start...");
    	Map<String,Object> vo = new HashMap<String,Object>();
		try {
			int a = service.updateOrderInfo(qo);
			if (a > 0) {
				vo.put("returnCode", 0);
				vo.put("saveNum", a);
				vo.put("msg", "更新成功！");
			} else {
				vo.put("returnCode", 999);
				vo.put("saveNum", a);
				vo.put("msg", "更新失败！");
			}
		} catch (Exception e) {
			logger.error("updateOrderInfo.do", e);
			vo.put("returnCode", 999);
			vo.put("saveNum", 0);
			vo.put("msg", "更新失败！");
		}
		return vo;
    }
}
